/*
 * Copyright (C) 2010 Imavis Srl
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.imavis.video.parse.mjpeg.impl;

import java.nio.ByteBuffer;

/**
 *
 * @author Aldo Brucale
 */
public enum JpegMarker {
    SOI(0xD8), // Start Of Image
    SOF0(0xC0), // Start Of Frame (Baseline DCT)
    SOF2(0xC2); // Start Of Frame (Progressive DCT)	
    private final int code;

    private JpegMarker(final int code) {
        this.code = code;
    }

    static int seekMarker(final ByteBuffer bb) {
        int prev = 0x00;
        while (bb.hasRemaining()) {
            final int nxt = (bb.get() & 0xff);
            if ((prev == 0xFF) && nxt != 00) {
                return nxt;
            } else {
                prev = nxt;
            }
        }
        return -1;
    }

    public boolean seek(final ByteBuffer bb) {
        int readCode;
        do {
            readCode = seekMarker(bb);
            if (readCode == code) {
                return true;
            }
        } while (readCode >= 0);

        return false;
    }
}
